import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
import chartsRouter from './modules/charts'
import tableRouter from './modules/table'
import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/system',
    component: Layout,
    redirect: 'noRedirect',
    alwaysShow: true,
    name: '/system',
    meta: {
      title: '系统管理',
      icon: 'lock'
    },
    children: [{
      path: 'dept',
      component: () => import('@/views/error-page/404'),
      name: '/system/dept',
      meta: {
        title: '科室管理',
        icon: 'edit'
      }
    }, {
      path: 'user',
      component: () => import('@/views/error-page/404'),
      name: '/system/user',
      meta: {
        title: '用户管理',
        icon: 'list'
      }
    }, {
      path: 'role',
      component: () => import('@/views/system/role/index'),
      name: '/system/role',
      meta: {
        title: '角色管理',
        icon: 'tab'
      }
    }, {
      path: 'menu',
      component: () => import('@/views/system/menu/index'),
      name: '/system/menu',
      meta: {
        title: '菜单管理',
        icon: 'bug'
      }
    }, {
      path: 'dict',
      component: () => import('@/views/system/dict/type'),
      name: '/system/dict',
      meta: {
        title: '字典管理',
        icon: 'zip'
      }
    }, {
      path: 'notice',
      component: () => import('@/views/error-page/404'),
      name: '/system/notice',
      meta: {
        title: '通知公告',
        icon: 'theme'
      }
    }, {
      path: 'log_login',
      component: () => import('@/views/error-page/404'),
      name: '/system/log_login',
      meta: {
        title: '登陆日志管理',
        icon: 'clipboard'
      }
    }, {
      path: 'log_opt',
      component: () => import('@/views/system/operLog/index'),
      name: '/system/log_opt',
      meta: {
        title: '操作日志管理',
        icon: 'clipboard'
      }
    }, {
      path: 'ins_fee',
      component: () => import('@/views/error-page/404'),
      name: '/system/ins_fee',
      meta: {
        title: '检查费用设置',
        icon: 'international'
      }
    }, {
      path: 'reg_fee',
      component: () => import('@/views/error-page/404'),
      name: '/system/reg_fee',
      meta: {
        title: '挂号费用设置',
        icon: 'international'
      }
    }]
  }, {
    path: '/statistics',
    component: Layout,
    redirect: 'noRedirect',
    name: '/statistics',
    meta: {
      title: '数据统计',
      icon: 'example'
    },
    children: [{
      path: 'revenue',
      component: () => import('@/views/error-page/404'),
      name: '/statistics/revenue',
      meta: {
        title: '收支统计',
        icon: 'edit'
      }
    }, {
      path: 'sales',
      component: () => import('@/views/error-page/404'),
      name: '/statistics/sales',
      meta: {
        title: '药品销售统计',
        icon: 'list'
      },
      hidden: true
    }, {
      path: 'check',
      component: () => import('@/views/error-page/404'),
      name: '/statistics/check',
      meta: {
        title: '检查项目统计',
        icon: 'list'
      }
    }, {
      path: 'workload',
      component: () => import('@/views/error-page/404'),
      name: '/statistics/workload',
      meta: {
        title: '工作量统计',
        icon: 'list'
      }
    }]
  }, {
    path: '/stock',
    component: Layout,
    redirect: 'noRedirect',
    name: '/stock',
    meta: {
      title: '药品进销存',
      icon: '404'
    },
    children: [{
      path: 'producter',
      component: () => import('@/views/error-page/404'),
      name: '/stock/producter',
      meta: {
        title: '生产厂家维护',
        icon: 'list'
      }
    }, {
      path: 'medicinal',
      component: () => import('@/views/error-page/404'),
      name: '/stock/medicinal',
      meta: {
        title: '药品信息维护',
        icon: 'list'
      }
    }, {
      path: 'provider',
      component: () => import('@/views/error-page/404'),
      name: '/stock/provider',
      meta: {
        title: '供应商维护',
        icon: 'list'
      }
    }, {

      path: 'purchase',
      component: () => import('@/views/error-page/404'),
      name: '/stock/purchase',
      meta: {
        title: '采购入库列表',
        icon: 'list'
      }
    }, {
      path: 'examine',
      component: () => import('@/views/error-page/404'),
      name: '/stock/examine',
      meta: {
        title: '入库审核',
        icon: 'list'
      }
    }, {
      path: 'inventory',
      component: () => import('@/views/error-page/404'),
      name: '/stock/inventory',
      meta: {
        title: '库存查询',
        icon: 'list'
      }
    }]
  }, {
    path: '/charge',
    component: Layout,
    redirect: 'noRedirect',
    name: '/charge',
    meta: {
      title: '收费管理',
      icon: 'excel'
    },
    children: [{
      path: 'charge',
      component: () => import('@/views/error-page/404'),
      name: '/charge/docharge',
      meta: {
        title: '处方收费',
        icon: 'list'
      }
    }, {
      path: 'chargelist',

      component: () => import('@/views/error-page/404'),
      name: '/charge/chargelist',
      meta: {
        title: '收费查询',
        icon: 'list'
      }
    }, {
      path: 'backfee',
      component: () => import('@/views/error-page/404'),
      name: '/charge/backfee',
      meta: {
        title: '处方退费',
        icon: 'list'
      }
    }, {
      path: 'backfeelist',
      component: () => import('@/views/error-page/404'),
      name: '/charge/backfeelist',
      meta: {
        title: '退费查询',
        icon: 'list'
      }
    }, {
      path: 'dispensing',
      component: () => import('@/views/error-page/404'),
      name: '/charge/dispensing',
      meta: {
        title: '处方发药',
        icon: 'list'
      }
    }]
  }, {
    path: '/check',
    component: Layout,
    redirect: 'noRedirect',
    name: '/check',
    meta: {
      title: '检查管理',
      icon: 'excel'
    },
    children: [{
      path: 'docheck',
      component: () => import('@/views/error-page/404'),
      name: '/check/docheck',
      meta: {
        title: '新开检查',
        icon: 'list'
      }
    }, {
      path: 'checkresult',
      component: () => import('@/views/error-page/404'),
      name: '/check/checkresult',
      meta: {
        title: '检查结果录入',
        icon: 'list'
      }
    }, {
      path: 'checklist',
      component: () => import('@/views/error-page/404'),
      name: '/check/checklist',
      meta: {
        title: '检查结果查询',
        icon: 'list'
      }
    }]
  }, {
    path: '/doctor',
    component: Layout,
    redirect: 'noRedirect',
    name: '/doctor',
    meta: {
      title: '看病就诊',
      icon: 'excel'
    },
    children: [{
      path: 'registered',
      component: () => import('@/views/error-page/404'),
      name: '/doctor/registered',
      meta: {
        title: '门诊挂号',
        icon: 'list'
      }
    }, {
      path: 'registeredlist',
      component: () => import('@/views/error-page/404'),
      name: '/doctor/registeredlist',
      meta: {
        title: '挂号列表',
        icon: 'list'
      }
    }, {
      path: 'newcare',
      component: () => import('@/views/error-page/404'),
      name: '/doctor/newcare',
      meta: {
        title: '新开就诊',
        icon: 'list'
      }
    }, {
      path: 'myscheduling',
      component: () => import('@/views/error-page/404'),
      name: '/doctor/myscheduling',
      meta: {
        title: '我的排班',
        icon: 'list'
      }
    }, {
      path: 'scheduling',
      component: () => import('@/views/error-page/404'),
      name: '/doctor/scheduling',
      meta: {
        title: '医生排班',
        icon: 'list'
      }
    }, {
      path: 'patient',
      component: () => import('@/views/error-page/404'),
      name: '/doctor/patient',
      meta: {
        title: '患者库',
        icon: 'list'
      }
    }]
  }, { // 系统自带系统管理面板
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, // will always show the root menu
    name: 'Permission',
    meta: {
      title: '权限管理',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/permission/page'),
        name: 'PagePermission',
        meta: {
          title: 'Page Permission',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'directive',
        component: () => import('@/views/permission/directive'),
        name: 'DirectivePermission',
        meta: {
          title: 'Directive Permission'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'role',
        component: () => import('@/views/permission/role'),
        name: 'RolePermission',
        meta: {
          title: 'Role Permission',
          roles: ['admin']
        }
      }
    ]
  },
  /** when your routing map is too long, you can split it into small modules **/
  // componentsRouter,
  // chartsRouter,
  nestedRouter
  // tableRouter,
]

export const lastRoute = [
  {
    path: '/dict',
    component: Layout,
    hidden: true,
    children: [{
      path: 'data/:dictId(\\d+)',

      component: () => import('@/views/system/dict/data'),
      name: '/dict',
      meta: {
        title: '数据字典'
      }
    }]
  }, {
    path: '/stock/purchase',
    component: Layout,
    hidden: true,
    children: [{
      path: 'insert',
      component: () => import('@/views/error-page/404'),
      name: '/stock/purchase/insert',
      meta: {
        title: '采购入库',
        icon: 'list'
      }
    }, {
      path: 'update/:purchaseId',
      component: () => import('@/views/error-page/404'),
      name: '/stock/purchase/update',
      meta: {
        title: '采购入库修改',
        icon: 'list'
      }
    }]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
